home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / TEXTILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  10.9 KB  |  428 lines

  1.  
  2. /* textile.c - by David Blythe, SGI */
  3.  
  4. /* The idea behind texture tiling is that OpenGL's texture modes,
  5.    particularly its support for a texture border make it possible to "tile"
  6.    together small textures with a result identical to if a much larger
  7.    texture was supported.  OpenGL allows implementations to limit the maximum 
  8.    size of a texture image (often this limit reflects size limitations within 
  9.    fast texturing hardware).  Texture tiling lets you work around otherwise
  10.    limited texture image sizes.
  11.  
  12.    Try textile with tiling enabled and linear filtering enabled.  As long as
  13.    you have texture borders enabled, you won't see any seams.  If you disable 
  14.    texture borders with linear filtering enabled, you'll get seams at the
  15.    boundaries of the tiles.  The seams can be detected whether the texture
  16.    wrap mode is either clamped or wrapped.
  17.  
  18.    If you disable texture tiling in textile, textile acts as if your maximum
  19.    texture image size was 32x32 (no matter what your OpenGL implementation
  20.    really supports) to mimic how the image would look on a system with a very 
  21.    limited texture image size.  When the display window is large, the
  22.    textured rectangle should look very blurry at this limited texture image
  23.    size. -mjk */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <string.h>
  29. #include <GL/glut.h>
  30. #include "texture.h"
  31.  
  32. int maxTextureSize;
  33. int maxTextureLevel;
  34.  
  35. int imageWidth, imageHeight;
  36. GLubyte *imageData;
  37.  
  38. int texWidthLevel0, texHeightLevel0;
  39. int texWidthTiles, texHeightTiles;
  40. GLubyte **texImageLevel;
  41.  
  42. GLboolean useBorder = GL_TRUE;
  43. GLboolean useClamp = GL_TRUE;
  44. GLboolean useLinear = GL_TRUE;
  45. GLboolean useMipmap = GL_TRUE;
  46. GLboolean useTextureTiling = GL_TRUE;
  47.  
  48. /* (int)floor(log2(a)) */
  49. static int
  50. iflog2(unsigned int a)
  51. {
  52.   int x = 0;
  53.   while (a >>= 1)
  54.     ++x;
  55.   return x;
  56. }
  57.  
  58. static void
  59. initialize(void)
  60. {
  61.   glMatrixMode(GL_PROJECTION);
  62.   glLoadIdentity();
  63.   glFrustum(-0.5, 0.5, -0.5, 0.5, 0.5, 1.5);
  64.  
  65.   glMatrixMode(GL_MODELVIEW);
  66.   glLoadIdentity();
  67.   glTranslatef(0, 0, -0.90);
  68.   glRotatef(45.0, 0, 1, 0);
  69.   glTranslatef(-0.5, -0.5, 0.0);
  70.  
  71. #if 0
  72.   /* A real program would query the real maximum supported texture size, but
  73.      program is an example.  Even better would be to use OpenGL 1.1's texture 
  74.      proxy mechanism. */
  75.   glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
  76. #else
  77.   /* Assume that the OpenGL implemenatation does not support anything larger
  78.      than 32x32 texture images. */
  79.   maxTextureSize = 32;
  80. #endif
  81.   maxTextureLevel = iflog2(maxTextureSize);
  82.  
  83.   texImageLevel = (GLubyte **) calloc(maxTextureLevel + 1, sizeof(GLubyte *));
  84.   if (texImageLevel == NULL) {
  85.     fprintf(stderr, "texture level image allocation failed\n");
  86.     exit(1);
  87.   }
  88.   glClearColor(0.1, 0.1, 0.1, 0.1);
  89. }
  90.  
  91. static void
  92. imgLoad(char *filename_in, int *w_out, int *h_out, GLubyte ** img_out)
  93. {
  94.   int comp;
  95.  
  96.   *img_out = (GLubyte *) read_texture(filename_in, w_out, h_out, &comp);
  97.   if (*img_out == NULL) {
  98.     fprintf(stderr, "unable to read %s\n", filename_in);
  99.     exit(1);
  100.   }
  101.   if (comp != 3 && comp != 4) {
  102.     fprintf(stderr, "%s: image is not RGB or RGBA\n", filename_in);
  103.     exit(1);
  104.   }
  105. }
  106.  
  107. static void
  108. buildMipmaps(void)
  109. {
  110.   int level, levelWidth, levelHeight;
  111.  
  112.   if (useTextureTiling) {
  113.     int width2 = iflog2(imageWidth);
  114.     int height2 = iflog2(imageHeight);
  115.  
  116.     width2 = (width2 > maxTextureLevel) ? width2 : maxTextureLevel;
  117.     height2 = (height2 > maxTextureLevel) ? height2 : maxTextureLevel;
  118.  
  119.     texWidthLevel0 = 1 << width2;
  120.     texHeightLevel0 = 1 << height2;
  121.     texWidthTiles = texWidthLevel0 >> maxTextureLevel;
  122.     texHeightTiles = texHeightLevel0 >> maxTextureLevel;
  123.   } else {
  124.     texWidthLevel0 = maxTextureSize;
  125.     texHeightLevel0 = maxTextureSize;
  126.     texWidthTiles = 1;
  127.     texHeightTiles = 1;
  128.   }
  129.  
  130.   texImageLevel[0] = (GLubyte *)
  131.     calloc(1, (texWidthLevel0 + 2) * (texHeightLevel0 + 2) * 4 * sizeof(GLubyte));
  132.  
  133.   glPixelStorei(GL_PACK_ROW_LENGTH, texWidthLevel0 + 2);
  134.   glPixelStorei(GL_PACK_SKIP_PIXELS, 1);
  135.   glPixelStorei(GL_PACK_SKIP_ROWS, 1);
  136.  
  137.   gluScaleImage(GL_RGBA, imageWidth, imageHeight,
  138.     GL_UNSIGNED_BYTE, imageData,
  139.     texWidthLevel0, texHeightLevel0,
  140.     GL_UNSIGNED_BYTE, texImageLevel[0]);
  141.  
  142.   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 1);
  143.   glPixelStorei(GL_UNPACK_SKIP_ROWS, 1);
  144.  
  145.   levelWidth = texWidthLevel0;
  146.   levelHeight = texHeightLevel0;
  147.   for (level = 0; level < maxTextureLevel; ++level) {
  148.     int newLevelWidth = (levelWidth > 1) ? levelWidth / 2 : 1;
  149.     int newLevelHeight = (levelHeight > 1) ? levelHeight / 2 : 1;
  150.  
  151.     texImageLevel[level + 1] = (GLubyte *)
  152.       calloc(1, (newLevelWidth + 2) * (newLevelHeight + 2) * 4 * sizeof(GLubyte));
  153.  
  154.     glPixelStorei(GL_PACK_ROW_LENGTH, newLevelWidth + 2);
  155.     glPixelStorei(GL_UNPACK_ROW_LENGTH, levelWidth + 2);
  156.  
  157.     gluScaleImage(GL_RGBA, levelWidth, levelHeight,
  158.       GL_UNSIGNED_BYTE, texImageLevel[level],
  159.       newLevelWidth, newLevelHeight,
  160.       GL_UNSIGNED_BYTE, texImageLevel[level + 1]);
  161.  
  162.     levelWidth = newLevelWidth;
  163.     levelHeight = newLevelHeight;
  164.   }
  165.  
  166.   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  167.   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  168.   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  169.  
  170.   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  171.   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  172.   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  173. }
  174.  
  175. static void
  176. freeMipmaps(void)
  177. {
  178.   int i;
  179.  
  180.   for (i = 0; i <= maxTextureLevel; ++i) {
  181.     if (texImageLevel[i] != NULL) {
  182.       free(texImageLevel[i]);
  183.       texImageLevel[i] = NULL;
  184.     }
  185.   }
  186. }
  187.  
  188. static void
  189. loadTile(int row, int col)
  190. {
  191.   int border = useBorder ? 1 : 0;
  192.   int level, levelWidth, levelHeight;
  193.  
  194.   levelWidth = texWidthLevel0;
  195.   levelHeight = texHeightLevel0;
  196.   for (level = 0; level <= maxTextureLevel; ++level) {
  197.     int tileWidth = levelWidth / texWidthTiles;
  198.     int tileHeight = levelHeight / texHeightTiles;
  199.     int skipPixels = col * tileWidth + (1 - border);
  200.     int skipRows = row * tileHeight + (1 - border);
  201.  
  202.     glPixelStorei(GL_UNPACK_ROW_LENGTH, levelWidth + 2);
  203.     glPixelStorei(GL_UNPACK_SKIP_PIXELS, skipPixels);
  204.     glPixelStorei(GL_UNPACK_SKIP_ROWS, skipRows);
  205.  
  206.     glTexImage2D(GL_TEXTURE_2D, level, 4,
  207.       tileWidth + 2 * border, tileHeight + 2 * border,
  208.       border, GL_RGBA, GL_UNSIGNED_BYTE, texImageLevel[level]);
  209.  
  210.     if (levelWidth > 1)
  211.       levelWidth = levelWidth / 2;
  212.     if (levelHeight > 1)
  213.       levelHeight = levelHeight / 2;
  214.   }
  215.  
  216.   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  217.   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  218.   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  219. }
  220.  
  221. static void
  222. redraw(void)
  223. {
  224.   GLenum minFilterMode, magFilterMode, wrapMode;
  225.   char *minFilterName, *magFilterName, *wrapName;
  226.   int i, j;
  227.  
  228.   if (useLinear) {
  229.     if (useMipmap) {
  230.       minFilterMode = GL_LINEAR_MIPMAP_LINEAR;
  231.       minFilterName = "LINEAR_MIPMAP_LINEAR";
  232.     } else {
  233.       minFilterMode = GL_LINEAR;
  234.       minFilterName = "LINEAR";
  235.     }
  236.     magFilterMode = GL_LINEAR;
  237.     magFilterName = "LINEAR";
  238.   } else {
  239.     if (useMipmap) {
  240.       minFilterMode = GL_NEAREST_MIPMAP_LINEAR;
  241.       minFilterName = "NEAREST_MIPMAP_LINEAR";
  242.     } else {
  243.       minFilterMode = GL_NEAREST;
  244.       minFilterName = "NEAREST";
  245.     }
  246.     magFilterMode = GL_NEAREST;
  247.     magFilterName = "NEAREST";
  248.   }
  249.  
  250.   if (useClamp) {
  251.     wrapMode = GL_CLAMP;
  252.     wrapName = "CLAMP";
  253.   } else {
  254.     wrapMode = GL_REPEAT;
  255.     wrapName = "REPEAT";
  256.   }
  257.  
  258.   fprintf(stderr, "tile(%s) ", useTextureTiling ? "yes" : "no");
  259.   fprintf(stderr, "border(%s) ", useBorder ? "yes" : "no");
  260.   fprintf(stderr, "filter(%s, %s) ", minFilterName, magFilterName);
  261.   fprintf(stderr, "wrap(%s) ", wrapName);
  262.   fprintf(stderr, "\n");
  263.  
  264.   glClear(GL_COLOR_BUFFER_BIT);
  265.  
  266.   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  267.  
  268.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilterMode);
  269.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilterMode);
  270.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
  271.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
  272.  
  273.   buildMipmaps();
  274.  
  275.   glEnable(GL_TEXTURE_2D);
  276.  
  277.   for (i = 0; i < texHeightTiles; ++i) {
  278.     float ySize = 1.0 / texHeightTiles;
  279.     float y0 = i * ySize;
  280.     float y1 = y0 + ySize;
  281.  
  282.     for (j = 0; j < texWidthTiles; ++j) {
  283.       float xSize = 1.0 / texWidthTiles;
  284.       float x0 = j * xSize;
  285.       float x1 = x0 + xSize;
  286.  
  287.       loadTile(i, j);
  288.  
  289.       glBegin(GL_TRIANGLE_STRIP);
  290.       glTexCoord2f(0.0, 1.0);
  291.       glVertex2f(x0, y1);
  292.       glTexCoord2f(0.0, 0.0);
  293.       glVertex2f(x0, y0);
  294.       glTexCoord2f(1.0, 1.0);
  295.       glVertex2f(x1, y1);
  296.       glTexCoord2f(1.0, 0.0);
  297.       glVertex2f(x1, y0);
  298.       glEnd();
  299.     }
  300.   }
  301.  
  302.   glDisable(GL_TEXTURE_2D);
  303.  
  304.   freeMipmaps();
  305. }
  306.  
  307. static void
  308. usage(char *name)
  309. {
  310.   fprintf(stderr, "\n");
  311.   fprintf(stderr, "usage: %s [ options ] filename\n", name);
  312.   fprintf(stderr, "\n");
  313.   fprintf(stderr, "    Demonstrates using texture borders\n");
  314.   fprintf(stderr, "    to tile a large texture\n");
  315.   fprintf(stderr, "\n");
  316.   fprintf(stderr, "  Options:\n");
  317.   fprintf(stderr, "    -sb  single buffered\n");
  318.   fprintf(stderr, "    -db  double buffered\n");
  319.   fprintf(stderr, "\n");
  320. }
  321.  
  322. /* ARGSUSED1 */
  323. void
  324. key(unsigned char key, int x, int y)
  325. {
  326.   switch (key) {
  327.   case '\033':
  328.     exit(0);
  329.     break;
  330.   case 'b':
  331.     useBorder = !useBorder;
  332.     break;
  333.   case 'w':
  334.     useClamp = !useClamp;
  335.     break;
  336.   case 'l':
  337.     useLinear = !useLinear;
  338.     break;
  339.   case 'm':
  340.     useMipmap = !useMipmap;
  341.     break;
  342.   case 't':
  343.     useTextureTiling = !useTextureTiling;
  344.     break;
  345.   default:
  346.     return;
  347.   }
  348.   glutPostRedisplay();
  349. }
  350.  
  351. void
  352. menu(int value)
  353. {
  354.   key((unsigned char) value, 0, 0);
  355. }
  356.  
  357. int doubleBuffered = GL_FALSE;
  358.  
  359. void 
  360. display(void)
  361. {
  362.   GLenum error;
  363.   redraw();
  364.   if (doubleBuffered)
  365.     glutSwapBuffers();
  366.   else
  367.     glFlush();
  368.   while ((error = glGetError()) != GL_NO_ERROR) {
  369.     fprintf(stderr, "Error: %s\n", (char *) gluErrorString(error));
  370.   }
  371. }
  372.  
  373. void
  374. reshape(int w, int h)
  375. {
  376.   glViewport(0, 0, w, h);
  377. }
  378.  
  379. int
  380. main(int argc, char *argv[])
  381. {
  382.   char *name = "Texture Tiling Test";
  383.   int width = 300, height = 300;
  384.   char *filename = NULL;
  385.   int i;
  386.  
  387.   for (i = 1; i < argc; ++i) {
  388.     if (!strcmp("-sb", argv[i])) {
  389.       doubleBuffered = GL_FALSE;
  390.  
  391.     } else if (!strcmp("-db", argv[i])) {
  392.       doubleBuffered = GL_TRUE;
  393.  
  394.     } else if (argv[i][0] != '-' && i == argc - 1) {
  395.       filename = argv[i];
  396.  
  397.     } else {
  398.       usage(argv[0]);
  399.       exit(1);
  400.     }
  401.   }
  402.  
  403.   if (filename == NULL) {
  404.     usage(argv[0]);
  405.     exit(1);
  406.   }
  407.   imgLoad(filename, &imageWidth, &imageHeight, &imageData);
  408.  
  409.   glutInitWindowSize(width, height);
  410.   glutInit(&argc, argv);
  411.   glutInitDisplayMode(GLUT_RGBA | (doubleBuffered ? GLUT_DOUBLE : 0));
  412.   glutCreateWindow(name);
  413.   glutDisplayFunc(display);
  414.   glutReshapeFunc(reshape);
  415.   glutKeyboardFunc(key);
  416.   initialize();
  417.   glutCreateMenu(menu);
  418.   glutAddMenuEntry("Toggle texture tiling", 't');
  419.   glutAddMenuEntry("Toggle clamping", 'w');
  420.   glutAddMenuEntry("Toggle linear filtering", 'l');
  421.   glutAddMenuEntry("Toggle mipmap usage", 'm');
  422.   glutAddMenuEntry("Toggle texture border", 'b');
  423.   glutAddMenuEntry("Quit", '\033');
  424.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  425.   glutMainLoop();
  426.   return 0;             /* ANSI C requires main to return int. */
  427. }
  428.